<template>
  <!-- 产品信息 -->
  <div>
    <curd-title v-if="infoFlg" :required="!['view'].includes(this.type)" :title="'产品信息'" />
    <avue-crud
      :table-loading="productloading"
      :option="productOption"
      :data="productList"
      @on-load="onProductLoad"
      @row-save="rowSaveProduct"
      @row-update="rowProductUpdate"
      @refresh-change="refreshProductChange"
      @row-del="rowProductDel"
      :row-class-name="delRowClassName"
    >
      <template slot="mainProducts" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.mainProductsFlag === '1'">{{ scope.row.mainProducts }}</del>
          {{ scope.row.mainProductsA }}
        </div>
        <span v-else>{{ scope.row.mainProducts }}</span>
      </template>
      <template slot="yearMaxCapacity" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.yearMaxCapacityFlag === '1'">{{ scope.row.yearMaxCapacity }}</del>
          {{ scope.row.yearMaxCapacityA }}
        </div>
        <span v-else>{{ scope.row.yearMaxCapacity }}</span>
      </template>
      <template slot="nowUserRatio" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.nowUserRatioFlag === '1'">{{ scope.row.nowUserRatio }}</del>
          {{ scope.row.nowUserRatioA }}
        </div>
        <span v-else>{{ scope.row.nowUserRatio }}</span>
      </template>
      <template slot="salesPreviousYear" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.salesPreviousYearFlag === '1'">{{ scope.row.salesPreviousYear }}</del>
          {{ scope.row.salesPreviousYearA }}
        </div>
        <span v-else>{{ scope.row.salesPreviousYear }}</span>
      </template>
      <template slot="salesLastYear" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.salesLastYearFlag === '1'">{{ scope.row.salesLastYear }}</del>
          {{ scope.row.salesLastYearA }}
        </div>
        <span v-else>{{ scope.row.salesLastYear }}</span>
      </template>
      <template slot="mainCustomers" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.mainCustomersFlag === '1'">{{ scope.row.mainCustomers }}</del>
          {{ scope.row.mainCustomersA }}
        </div>
        <span v-else>{{ scope.row.mainCustomers }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { productOption } from '@/option/tenant/supplier/factory-info'
import { getStore } from '@/util/store'
import { getProductList, removeProduct, addProduct, updateProduct } from '@/api/supplier/product'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'ProductInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      // 产品信息
      productloading: false,
      productList: [],
      productOption: productOption.call(this)
    }
  },
  methods: {
    // 产品信息
    onProductLoad() {
      this.infoFlg
        ? ((this.productloading = true),
          getProductList({ supplierId: this.supplierId }).then(res => {
            this.productList = res.data.data
            this.productloading = false
          }))
        : ''
    },
    refreshProductChange() {
      this.onProductLoad()
    },
    rowSaveProduct(row, done, loading) {
      addProduct({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowProductUpdate(row, index, done, loading) {
      updateProduct(row).then(
        () => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowProductDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeProduct(row.id)
        })
        .then(() => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
